<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\adminService;
use think\Db;
use auth\Auth;
use think\Session;
class Admin extends Common{
    public function fetchdata() {
        $type = request()->post('type');
        switch($type) {
            case 1:
                $usertype = request()->post('usertype');
                if($usertype == 1){
                    //查询小区信息
                    $department = (new adminService())->serviceInfo();
                    $arr = [];
                    foreach ($department as $val) {
                        $arr[] = $val['name'];
                    }
                }else if($usertype == 2){
                    //查询物业信息
                    $department = (new adminService())->propertyInfo();
                    $arr = [];
                    foreach ($department as $val) {
                        $arr[] = $val['name'];
                    }
                }else if($usertype == 3){
                    //查询小区信息
                    $department = (new adminService())->communityInfo();
                    $arr = [];
                    foreach ($department as $val) {
                        $arr[] = $val['name'];
                    }
                }
                return array_values($arr);
            default:
                return json(['info' => '查询错误']);
        }
    }
    public function adminList(){
        //查询管理员数据
        $lists = (new adminService())->adminList();
        //根据角色ID查询角色名称
        foreach($lists as $lis){
            //查询部门名称
            if($lis['usertype'] == 0){
                $department = '早早科技有限公司';
            }else if($lis['usertype'] == 1){
                $department = (new adminService())->propertyIdInfo($lis['department']);
            }else if($lis['usertype'] == 2){
                $department = (new adminService())->communityIdInfo($lis['department']);
            }else{
                $department = '未定义部门';
            }
            $lis['department'] = $department;
            //查询角色名称
            $_groupTitle = (new Auth())->getGroups($lis['id']);
            if($_groupTitle){
                $groupTitle = $_groupTitle[0]['title']; 
            }else{
                $groupTitle = '用户未选择角色';
            }
            $lis['group'] = $groupTitle;
            $list[] = $lis;
        }
        $this->assign('list',$list);
        //查询管理员数据条数
        $number = (new adminService())->adminNumber();
        $number = count($number);
        $this->assign('number',$number);
        return $this->fetch('admin_list');
    }
    public function adminAdd(){
        //查询角色数据
        $group = (new adminService())->authGroup();
        $this->assign('group',$group);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //验证表单信息
            $validate = $this->validate($data,[
                'name|名称' => 'require' ,
                'password|密码' => 'require|alphaNum',
                'group|角色' => 'require',
                'usertype|用户类型' => 'require',
                'department|所属部门' => 'require'
            ]);
            if($validate !== true){
                $this->error($validate);
            }
            //判断账号名称是否重复
            $info = (new adminService())->adminName($data['name']);
            if($info){
                $this->error('账号已存在');
            }
            //将密码md5加密
            $data['password'] = md5($data['password']);
            $data['time'] = date('Y-m-d H:i:s',time());
            $juese['group_id'] = $data['group'];
            unset($data['group']);
            unset($data['repass']);
            //根据部门名称查询ID
            if($data['usertype'] == 1){
                $data['department'] = (new adminService())->serviceNameId($data['department']);
            }else if($data['usertype'] == 2){
                $data['department'] = (new adminService())->propertyNameId($data['department']);
            }else if($data['usertype'] == 3){
                $data['department'] = (new adminService())->communityNameId($data['department']);
            }
            //将数据插入数据库
            $result = (new adminService())->adminAdd($data);
            //判断数据是否插入成功
            if($result){
                $juese['uid'] = $result;
                $admingroup = (new adminService())->adminGroup($juese);
                $this->success('添加管理员成功',url('adminList'));
            }else{
                $this->error('添加管理员失败',url('adminAdd'));
            }
        }else{
            return $this->fetch('admin_add');
        }
    }
    public function adminDel(){
		$request = request();
		$id = $request->param('id');
		$validate = $this->validate($request->param(),[
			'id'	=>	'require|number'
		]);
		if($validate !== true){
			$this->error($validate);
        }
        //判断是否存在该数据
        $info = (new adminService())->adminId($id);
        if(empty($info)){
            $this->error('该管理员不存在');
		}
        $result = Db::name('admin')->delete($id);
        if($result){
            $groupId = Db::name('auth_group_access')->where('uid',$id)->delete();
            $this->success('删除管理员成功',url('adminList'));
		}else{
            $this->success('删除管理员失败',url('adminList'));
		}
    }
}